﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		ItemList.cs
//
//	Author:		MJP
//
//	Date:		04/26/09
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using System.Reflection;
using System.Windows.Forms;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

using CPXMenu;

using CPXMenuEditor.Forms;
using CPXMenuEditor.Controls;

using CPXMenuItem = CPXMenu.MenuItem;
using WFMenuItem = System.Windows.Forms.MenuItem;

namespace CPXMenuEditor.Controls
{
    public partial class ItemList : UserControl
    {
        private Dictionary<String, TreeNode> categoryNodes = new Dictionary<string, TreeNode>();
        private Type selectedType = null;
        private bool allowItemDragging = false;

        [Browsable(false)]
        public Type SelectedType
        {
            get { return selectedType; }
        }

        [Browsable(true)]
        [Category("Behavior")]
        [Description("Controls whether types can be dragged out of the control")]
        public bool AllowItemDragging
        {
            get { return allowItemDragging; }
            set { allowItemDragging = value; }
        }

        public ItemList()
        {
            InitializeComponent();

            treeView.ImageList = MenuTree.GlobalIconList;

            InitializeTree(null);
        }

        public void InitializeTree(CPXMenuItem parent)
        {
            treeView.Nodes.Clear();
            categoryNodes.Clear();

            if (parent != null)
                Debug.Assert(CPXMenuItem.TypeCanHaveChildren(parent.GetType()));

            Assembly assembly = Assembly.GetAssembly(typeof(CPXMenuItem));
            Type[] types = assembly.GetTypes();
            foreach (Type type in types)
            {
                if (CPXMenuItem.IsMenuItemType(type) && !type.IsAbstract && type != typeof(MenuProject))
                {
                    if (parent == null || CPXMenuItem.IsValidParentChildCombination(parent.GetType(), type))
                    {
                        String category = CPXMenuItem.TypeCategory(type);
                        TreeNode parentNode;
                        if (categoryNodes.ContainsKey(category))
                        {
                            parentNode = categoryNodes[category];
                        }
                        else
                        {
                            parentNode = new TreeNode(category);
                            parentNode.Tag = "Category";
                            parentNode.Text = category;
                            parentNode.ImageKey = "FolderOpen";
                            parentNode.SelectedImageKey = "FolderOpen";
                            treeView.Nodes.Add(parentNode);
                            categoryNodes.Add(category, parentNode);
                        }


                        TreeNode typeNode = new TreeNode(type.Name);
                        typeNode.Tag = type;
                        typeNode.Text = type.Name;
                        typeNode.ImageKey = CPXMenuItem.GetIconKey(type);
                        typeNode.SelectedImageKey = typeNode.ImageKey;
                        parentNode.Nodes.Add(typeNode);
                        if (selectedType == null)
                        {
                            treeView.SelectedNode = typeNode;
                            selectedType = type;
                        }
                    }
                }
            }
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag is Type)
            {
                selectedType = (Type)e.Node.Tag;
                descriptionTextBox.Text = CPXMenuItem.TypeDescription(selectedType);
            }
            else
                treeView.SelectedNode = e.Node.Nodes[0];
        }        

        private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (allowItemDragging)
            {
                if (e.Button == MouseButtons.Left)
                {
                    TreeNode node = (TreeNode)e.Item;

                    if (node.Tag is Type)
                        DoDragDrop(node.Tag.ToString(), DragDropEffects.Move);
                }
            }
        }
    }
}
